package com.wswy.wzcx.ui.persenter;

import android.app.DatePickerDialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.widget.DatePicker;
import android.widget.EditText;

import com.che.libcommon.utils.ToastUtils;
import com.che.libcommon.utils.optional.Optional;
import com.wswy.wzcx.api.Api;
import com.wswy.wzcx.model.BillType;
import com.wswy.wzcx.model.GenreChild;
import com.wswy.wzcx.ui.other.DialogFactory;
import com.wswy.wzcx.ui.view.AddBillView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import io.reactivex.SingleObserver;

/**
 * Created by wangduheng26 on 2018/2/5.
 * wangduheng26@gmail.com
 */

public class AddBillPresenter {
    private SimpleDateFormat dateformat1 = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);

    private AddBillView mView;

    public AddBillPresenter(AddBillView view) {
        this.mView = view;
    }

    public String format(Calendar calendar) {
        if (calendar == null) calendar = Calendar.getInstance();
        return dateformat1.format(calendar.getTime());

    }

    public void save(SingleObserver<Optional<Object>> observer, BillType currentType, EditText edAmount, EditText edDesc, String dateStr, int id) {

        if (currentType == null) {
            ToastUtils.showText("选择一个消费类型吧");
            return;
        }
        Editable text = edAmount.getText();
        if (TextUtils.isEmpty(text)) {
            ToastUtils.showText("填写一个消费金额吧！");
            edAmount.requestFocus();
            return;
        }
        Editable desc = edDesc.getText();
        String bookDesc = null;
        if (TextUtils.isEmpty(desc)) {
//            ToastUtils.showText("填写一个消费备注信息吧！");
//            edDesc.requestFocus();
//            return;
            bookDesc="";
        }else {
            bookDesc = desc.toString();
        }
        Api.get().submitBill(String.valueOf(text), currentType.content, bookDesc, dateStr, id)
                .subscribe(observer);
    }

    public void delete(SingleObserver<Optional<Object>> observer, GenreChild child) {
        if (child != null) {
            Api.get().submitDel(child.id)
                    .subscribe(observer);
        }
    }

    public void showDatePicker(final DateSetCallBack callBack) {
        Context context = mView.activity();
        Calendar calendar = Calendar.getInstance();
        int year = Math.max(calendar.get(Calendar.YEAR), 2017);
        int month = calendar.get(Calendar.MONTH);
        int dayOfM = calendar.get(Calendar.DAY_OF_MONTH);
        DatePickerDialog datePickerDialog = DialogFactory.createDatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                callBack.callback(year, month, dayOfMonth);


            }
        }, year, month, dayOfM);
        DatePicker datePicker = datePickerDialog.getDatePicker();
        datePicker.setMaxDate(calendar.getTimeInMillis());
        datePickerDialog.show();
    }

    public Calendar parseDate(String createTime) throws ParseException {
        Date parse = dateformat1.parse(createTime);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(parse);
        return calendar;
    }

    public interface DateSetCallBack {
        void callback(int year, int month, int dayOfMonth);
    }
}
